package com.atguigu.jxc.controller;

import com.atguigu.jxc.domain.ServiceVO;
import com.atguigu.jxc.entity.Goods;
import com.atguigu.jxc.service.GoodsService;
import com.atguigu.jxc.util.PageResult;
import com.atguigu.jxc.vo.GoodsByNameAndTypeIdVo;
import com.atguigu.jxc.vo.GoodsListVo;
import com.atguigu.jxc.vo.SaveStockVO;
import com.atguigu.jxc.vo.SearchVo;
import com.github.pagehelper.PageInfo;
import org.apache.shiro.authz.annotation.Logical;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @description 商品信息Controller
 */
@RestController
@RequestMapping("goods")
public class GoodsController {

    @Autowired
    private GoodsService goodsService;

    /**
     * 分页查询商品库存信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param codeOrName 商品编码或名称
     * @param goodsTypeId 商品类别ID
     * @return
     */


    /**
     * 分页查询商品信息
     * @param page 当前页
     * @param rows 每页显示条数
     * @param goodsName 商品名称
     * @param goodsTypeId 商品类别ID
     * @return
     */


    /**
     * 生成商品编码
     * @return
     */
    @RequestMapping("/getCode")
    @RequiresPermissions(value = "商品管理")
    public ServiceVO getCode() {
        return goodsService.getCode();
    }


    /**
     * 查询当前库存（可根据商品类别、商品编码或名称搜索）
     * @param
     * @return
     */
    @PostMapping("/listInventory")
    public Map<String,Object> listInventory(GoodsListVo goodsListV){
        PageResult pageResult = goodsService.listInventory(goodsListV);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("rows",pageResult.getRows());
        return map;
    }

    /**
     * 查询所有商品信息
     * @param goodsByNameAndTypeIdVo
     * @return
     */
    @PostMapping("list")
    public Map<String, Object> goodsListByNameAndTypeId(GoodsByNameAndTypeIdVo goodsByNameAndTypeIdVo){
        PageResult pageResult = goodsService.goodsListByNameAndTypeId(goodsByNameAndTypeIdVo);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("rows",pageResult.getRows());
        return map;
    }

    /**
     * 商品添加或修改
     * @param goodsId
     * @param goods
     * @return
     */
    @PostMapping("save")
    public ServiceVO saveGoods(@RequestParam(required = false) Integer goodsId ,Goods goods){
        goodsService.saveGoods(goodsId,goods);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 商品删除
     * @param goodsId
     * @return
     */
    @PostMapping("delete")
    public ServiceVO deleteGoods(@RequestParam Integer goodsId){
        if (goodsId != null) {
            Boolean aBoolean = goodsService.deleteGoodsById(goodsId);
            if (aBoolean) {
                return new ServiceVO(100, "请求成功", null);
            }
        }
        return new ServiceVO(401,"请求失败",null);
    }

    /**
     * 无库存商品列表展示（可以根据商品名称或编码查询）
     */
    @PostMapping("getNoInventoryQuantity")
    public Map<String, Object> getNoInventoryQuantity(SearchVo searchVo){
        PageResult pageResult = goodsService.getNoInventoryQuantity(searchVo);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("rows",pageResult.getRows());
        return map;
    }

    /**
     * 有库存商品列表展示（可以根据商品名称或编码查询）
     * @param searchVo
     * @return
     */
    @PostMapping("getHasInventoryQuantity")
    public Map<String, Object> getHasInventoryQuantity(SearchVo searchVo){
        PageResult pageResult = goodsService.getHasInventoryQuantity(searchVo);
        Map<String,Object> map = new HashMap<>();
        map.put("total",pageResult.getTotal());
        map.put("rows",pageResult.getRows());
        return map;
    }


    /**
     * 添加库存、修改数量或成本价
     * @param saveStockVO
     * @return
     */
    @PostMapping("saveStock")
    public ServiceVO saveStock(SaveStockVO saveStockVO){
        goodsService.saveStock(saveStockVO);
        return new ServiceVO(100,"请求成功",null);
    }

    /**
     * 删除库存（要判断商品状态 入库、有进货和销售单据的不能删除）
     * @param goodsId
     * @return
     */
    @PostMapping("deleteStock")
    public ServiceVO deleteStock(@RequestParam Integer goodsId){
        Boolean aBoolean = goodsService.deleteStock(goodsId);
        if (aBoolean){
             return new ServiceVO(100,"请求成功",null);
        }else {
            return new ServiceVO(401,"删除失败",null);
        }
    }

    /**
     * 查询所有 当前库存量 小于 库存下限的商品信息
     */
    @PostMapping("listAlarm")
    public Map<String,Object> listAlarm(){
       List<Goods> goodsList =  goodsService.listAlarm();
       Map<String, Object> params = new HashMap<>();
       params.put("rows",goodsList);
       return params;
    }
}
